﻿using System;
using System.Collections.Generic;
using System.Data;
using eTMS.Controllers.BoMonService;
using eTMS.Controllers.GiaoVienService;
using eTMS.Controllers.PhanCongGiangDayService;
using eTMS.Controllers.MonHocService;

namespace eTMS.Controllers
{
    public class GiaoVienController : GiaoVienControllerBase
    {
        public GiaoVienController()
            : base()
        {
        }
        public bool SaveGiaoVienTable()
        {
            bool result = true;
            foreach (DataRow row in Table.Rows)
            {
                GiaoVienObject = MapDataRowToObject(row);
                if (GiaoVienObject.GiaoVien_ID == 0)
                {
                    result &= AddNew();
                }
            }
            return result;
        }
        public void ResetTable()
        {
            Table = new DataTable();
            Table.Columns.Add("GIAOVIEN_ID", typeof(int));
            Table.Columns.Add("MaGiaoVien");
            Table.Columns.Add("HoTenGiaoVien");
            Table.Columns.Add("NgaySinh", typeof(DateTime));
            Table.Columns.Add("DiaChi");
            Table.Columns.Add("DienThoai");
            Table.Columns.Add("GioiTinh",typeof(byte));
            Table.Columns.Add("Email");
            Table.Columns.Add("BoMon_ID");
        }

        private DataTable _tblChiTiet;
        public DataTable TableChiTiet
        {
            get
            {
                return _tblChiTiet;
            }
        }

        public void LoadChiTiet(int GiaoVien_ID)
        {
            PhanCongGiangDayService.PhanCongGiangDayServiceClient w = new PhanCongGiangDayService.PhanCongGiangDayServiceClient();
            _tblChiTiet = w.SelectAllDSWithMonHocByGiangVien(GiaoVien_ID).Tables[0];
            _tblChiTietRemoved = _tblChiTiet.Clone();
        }
        private DataTable _tblChiTietRemoved;
        public void RemoveMonHocFromChiTiet(List<DataRow> lsRow)
        {
            foreach (DataRow r in lsRow)
            {
                _tblChiTietRemoved.ImportRow(r);
                _tblChiTiet.Rows.Remove(r);
            }
        }
        public void AddMonHocToChiTiet(MonHocEntity oMonHoc)
        {
            DataRow[] rs = _tblChiTietRemoved.Select("MonHoc_ID=" + oMonHoc.MonHoc_ID.ToString());
            if (rs.Length > 0)
            {
                _tblChiTiet.ImportRow(rs[0]);
            }
            else
            {
                DataRow r = _tblChiTiet.NewRow();
                r["TenMonHoc"] = oMonHoc.TenMonHoc;
                r["MonHoc_ID"] = oMonHoc.MonHoc_ID;
                r["MaMonHoc"] = oMonHoc.MaMonHoc;
                _tblChiTiet.Rows.Add(r);
            }
        }
        public void SaveChiTiet()
        {
            PhanCongGiangDayController phanCongGiangDayController = new PhanCongGiangDayController();
            foreach (DataRow row in _tblChiTiet.Rows)
            {
                phanCongGiangDayController.ResetObject();
                phanCongGiangDayController.PhanCongGiangDayObject = phanCongGiangDayController.MapDataRowToObject(row);
                phanCongGiangDayController.PhanCongGiangDayObject.GiaoVien_ID = GiaoVienObject.GiaoVien_ID;
                if (phanCongGiangDayController.PhanCongGiangDayObject.PhanCongGiangDay_ID > 0)
                {

                    phanCongGiangDayController.Update();
                }
                else
                {
                    phanCongGiangDayController.AddNew();

                }
            }
            foreach (DataRow row in _tblChiTietRemoved.Rows)
            {
                phanCongGiangDayController.ResetObject();
                phanCongGiangDayController.PhanCongGiangDayObject = phanCongGiangDayController.MapDataRowToObject(row);
                if (phanCongGiangDayController.PhanCongGiangDayObject.PhanCongGiangDay_ID > 0)
                {
                    phanCongGiangDayController.Delete();
                }
            }
        }


        public static List<GiaoVienEntity> GetGiaoVienInKhoa(int Khoa_ID)
        {
            List<BoMonEntity> listBoMon = BoMonController.ListBoMonStatic.FindAll(delegate(BoMonEntity oFind) { return oFind.Khoa_ID.Equals(Khoa_ID); });
            return ListGiaoVienStatic.FindAll(delegate(GiaoVienEntity oFind) { return listBoMon.Find(delegate(BoMonEntity oBoMon) { return oFind.BoMon_ID.Equals(oBoMon.BoMon_ID); }) != null; });
        }
        public static List<GiaoVienEntity> GetGiaoVienInList(List<int> lsGiaoVien_ID)
        {
            return ListGiaoVienStatic.FindAll(delegate(GiaoVienEntity oFind) { return lsGiaoVien_ID.Contains(oFind.GiaoVien_ID); });
        }
        public static GiaoVienEntity GetGiaoVienByID(int GiaoVien_ID)
        {
            return ListGiaoVienStatic.Find(delegate(GiaoVienEntity oFind) { return oFind.GiaoVien_ID == GiaoVien_ID; });
        }
    }
}
